//
//  UIBarButtonItem+Category.swift
//  Super_WeiBo
//
//  Created by SuperMac on 16/3/16.
//  Copyright © 2016年 lc. All rights reserved.
//

import UIKit

extension  UIBarButtonItem{
    
    class func creatBarBtn(imageName : String , target: AnyObject? , action: Selector) -> UIBarButtonItem{
        
        let btn = UIButton()
        btn.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: imageName + "_highlighted"), forState: UIControlState.Highlighted)
        btn.addTarget(target, action: action, forControlEvents: UIControlEvents.TouchUpInside)
        btn.sizeToFit()
        return UIBarButtonItem(customView: btn)
        
    }
    
    /**
     便利构造方法
     */
    convenience init(imageName : String , target: AnyObject? , action: String?) {
        
        let btn = UIButton()
        btn.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: imageName + "_highlighted"), forState: UIControlState.Highlighted)
        if action != nil
        {
            // 如果是自己封装一个按钮, 最好传入字符串, 然后再将字符串包装为Selector
            btn.addTarget(target, action: Selector(action!), forControlEvents: UIControlEvents.TouchUpInside)
        }
        btn.sizeToFit()
        self.init(customView: btn)
        
    }
    
}